﻿<%@ WebHandler Language="C#" Class="ReportTracking.Services.CbDVKDVT" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Web.UI;
using System.Data;

namespace ReportTracking.Services
{
    /// <summary>
    /// Summary description for CbDVKDVT
    /// </summary>
    public class CbDVKDVT : IHttpHandler, IReadOnlySessionState
    {


        public void ProcessRequest(HttpContext context)
        {


            try
            {
                context.Response.ContentType = "text/plain";
                DateTime fd = Convert.ToDateTime(context.Request.QueryString["fd"]);
                DateTime td = Convert.ToDateTime(context.Request.QueryString["td"]);
                StringBuilder sData = new StringBuilder();
                Tracking.Web.Library.TrackingReportService.TrackingReportClient rptrk = new Tracking.Web.Library.TrackingReportService.TrackingReportClient();
                DataSet ds = rptrk.GetBusinessUnit(fd, td);
                if (ds != null)
                {
                    sData.Append("[{");
                    sData.Append("'id': 0,");
                    sData.Append("'text': 'All',");
                    sData.Append("'iconCls':'icon-ok',");
                    sData.Append("'checked':false,");
                    sData.Append("'children':[");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        sData.Append("{");
                        sData.Append("'id':");
                        sData.Append("'");
                        sData.Append((i + 1));
                        sData.Append("',");
                        sData.Append("'text':");
                        sData.Append("'");
                        sData.Append(ds.Tables[0].Rows[i][0]);
                        sData.Append("',");
                        sData.Append("'checked':false,");
                        sData.Append("}");
                        sData.Append(",");
                    }
                    sData.Replace(",", "", sData.Length - 2, 1);
                    sData.Append("]");
                }
                else
                {
                    sData.Append("[{");
                    sData.Append("'id': 0,");
                    sData.Append("'text': 'All',");
                    sData.Append("'iconCls':'icon-ok',");
                }

                sData.Append("}]");
                context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));
               /* DataTable tb = TaoTable();
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(tb));*/
            }
            catch (Exception)
            {

                throw;
            }


        }

        private DataTable TaoTable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("A", typeof(string));
            tb.Columns.Add("B", typeof(int));
            for (int i = 0; i < 3; i++)
            {
                tb.Rows.Add((i + 1), (i + 1));
            }
            return tb;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}